package com.shanghai.shanghaielectric.utils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Calendar;

/**
 * Created by admin on 2018/1/9.
 */

public class CommUtil {

    //二进制获取高次谐波显示
    public static String getHighHarmonic(String data){
        String back = "";
        back = back + (String.valueOf(data.charAt(15)).equals("0") ? "" : "19,");
        back = back + (String.valueOf(data.charAt(14)).equals("0") ? "" : "21,");
        back = back + (String.valueOf(data.charAt(13)).equals("0") ? "" : "23,");
        back = back + (String.valueOf(data.charAt(12)).equals("0") ? "" : "25,");
        back = back + (String.valueOf(data.charAt(11)).equals("0") ? "" : "27,");
        back = back + (String.valueOf(data.charAt(10)).equals("0") ? "" : "29,");
        back = back + (String.valueOf(data.charAt(9)).equals("0") ? "" : "31,");
        back = back + (String.valueOf(data.charAt(8)).equals("0") ? "" : "33,");
        back = back + (String.valueOf(data.charAt(7)).equals("0") ? "" : "35,");
        back = back + (String.valueOf(data.charAt(6)).equals("0") ? "" : "37,");
        back = back + (String.valueOf(data.charAt(5)).equals("0") ? "" : "39,");
        back = back + (String.valueOf(data.charAt(4)).equals("0") ? "" : "41,");
        back = back + (String.valueOf(data.charAt(3)).equals("0") ? "" : "43,");
        back = back + (String.valueOf(data.charAt(2)).equals("0") ? "" : "45,");
        back = back + (String.valueOf(data.charAt(1)).equals("0") ? "" : "47,");
        back = back + (String.valueOf(data.charAt(0)).equals("0") ? "" : "49");
        return back;
    }

    /**
     * 对double数据进行取精度.
     * @param value  double数据.
     * @param scale  精度位数(保留的小数位数).
     * @param roundingMode  精度取值方式.
     * @return 精度计算后的数据.
     */
    public static double round(double value, int scale,
                               int roundingMode) {
        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(scale, roundingMode);
        double d = bd.doubleValue();
        bd = null;
        return d;
    }

    public static String dataDot(double data,int dot){
        String format = "0.";
        if(dot == 0){
            format = "0";
        }
        for (int i = 0; i < dot; i++) {
            format = format + "0";
        }
        DecimalFormat decimalFormat = new DecimalFormat(format);
        return decimalFormat.format(data);
    }

    public static String binaryString2hexString(String bString)
    {
        if (bString == null || bString.equals("") || bString.length() % 8 != 0)
            return null;
        StringBuffer tmp = new StringBuffer();
        int iTmp = 0;
        for (int i = 0; i < bString.length(); i += 4)
        {
            iTmp = 0;
            for (int j = 0; j < 4; j++)
            {
                iTmp += Integer.parseInt(bString.substring(i + j, i + j + 1)) << (4 - j - 1);
            }
            tmp.append(Integer.toHexString(iTmp));
        }
        return tmp.toString();
    }

    public static String hexString2binaryString(String hexString) {
        if (hexString == null || hexString.length() % 2 != 0)
            return null;
        String bString = "", tmp;
        for (int i = 0; i < hexString.length(); i++) {
            tmp = "0000"
                    + Integer.toBinaryString(Integer.parseInt(
                    hexString.substring(i, i + 1), 16));
            bString += tmp.substring(tmp.length() - 4);
        }
        return bString;
    }

    public static int oneByteToInt(byte data){
        byte[] buf = new byte[1];
        buf[0] = data;
        int result = Integer.parseInt(bytesToHexString(buf).trim(), 16);
        return result;
    }

    public static int twoByteToInt(byte firstByte,byte secondByte){
        byte[] buf = new byte[2];
        buf[0] = firstByte;
        buf[1] = secondByte;
        int result = Integer.parseInt(bytesToHexString(buf).trim(), 16);
        return result;
    }

    public static String IntToTwoByteHexString(int data){
        byte[] buf = new byte[2];
        buf[0] = (byte) (data >> 8);
        buf[1] = (byte) data;
        return bytesToHexString(buf);
    }

    public static long getDayLongTime(){
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DATE);
        return getLongTime(year, month, day);
    }

    public static long getLongTime(int year,int month,int day){
        Calendar c = Calendar.getInstance();
        c.set(year, month - 1,day,0,0,0);
        return (c.getTimeInMillis() / 1000)*1000;
    }

    public static long getCurrentTime(){
        return (System.currentTimeMillis() / 1000)*1000;
    }

    public static int calDays(int year, int month) {
        boolean leayyear = false;
        int day = 1;
        if (year % 4 == 0 && year % 100 != 0) {
            leayyear = true;
        } else {
            leayyear = false;
        }
        for (int i = 1; i <= 12; i++) {
            switch (month) {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    day = 31;
                    break;
                case 2:
                    if (leayyear) {
                        day = 29;
                    } else {
                        day = 28;
                    }
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    day = 30;
                    break;
            }
        }
        return day;
    }

    /**
     * 将指定字符串src，以每两个字符分割转换为16进制形式 如："2B44EFD9" --> byte[]{0x2B, 0x44, 0xEF,
     * 0xD9}
     *
     * @param src
     *            String
     * @return byte[]
     */
    public static byte[] HexString2Bytes(String src) {
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < tmp.length / 2; i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }

    /**
     * 将两个ASCII字符合成一个字节； 如："EF"--> 0xEF
     *
     * @param src0
     *            byte
     * @param src1
     *            byte
     * @return byte
     */
    private static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
                .byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
                .byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }

    /**
     * 将Hex数组转换为Hex字符串
     *
     * @param src
     * @param size
     * @return
     */
    public static String bytesToHexString(byte[] src) {
        String ret = "";
        if (src == null) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            String hex = Integer.toHexString(src[i] & 0xFF);
            if (hex.length() < 2) {
                hex = "0" + hex;
            }
            ret += hex;
        }
        return ret.toUpperCase();
    }

    /**
     * 将Hex数组转换为Hex字符串
     *
     * @param src
     * @param size
     * @return
     */
    public static String bytesTo_HexString(byte[] src) {
        String ret = "";
        if (src == null) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            String hex = Integer.toHexString(src[i] & 0xFF);
            if (hex.length() < 2) {
                hex = "0" + hex;
            }
            ret += hex+" ";
        }
        return ret.toUpperCase();
    }
}

